<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h1>登录页面</h1>
    <a href="index.html">跳转注册页面</a>
<form>
    <input type="text" placeholder="用户名" name="" id="">
    <input type="text" placeholder="密码">
    <input type="submit" value="登录">
</form>
<script>
    var inps = document.querySelectorAll("input");
    document.forms[0].onsubmit = function (e) {
        e.preventDefault();

        var xhr = new XMLHttpRequest();
        xhr.open("post","/login");
        xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

        var u = inps[0].value;
        var p = inps[1].value;

        xhr.send(`username=${u}&password=${p}`)
        xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
    
            console.log(xhr.responseText);
            var res = JSON.parse(xhr.responseText);
            console.log(res); 
        }
        }

    }
</script>
</body>

</html>